package cn.wh.basic.controller;

import cn.wh.basic.dto.RegisterDto;
import cn.wh.basic.exception.BusinessException;
import cn.wh.basic.service.IVerifyCodeSerivce;
import cn.wh.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeSerivce iVerifyCodeSerivce;

    @GetMapping("/image/{key}")
    public AjaxResult VerifyCode(@PathVariable("key") String key){
        try {
            String imageCode = iVerifyCodeSerivce.imageCode(key);
            return AjaxResult.my().setResultObj(imageCode);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }


    @PostMapping("/smsCode")
    public AjaxResult VerifyCode(@RequestBody RegisterDto dto){
        try {
            iVerifyCodeSerivce.smsCode(dto);
            return AjaxResult.my();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }
}
